Ext.define('appmod.controller.knowledge.content.PostInfoStartup', {
    extend: 'Ext.app.Controller',

    views: [
		'knowledge.content.PostInfoForm'
    ],

	models : [
	],
	
    stores: [
		'knowledge.category.CurrentTree'
	],	

    refs: [
		{ref: 'postInfoForm',selector:'knowledge-content-postinfoform'}

		

	],

    init: function() {
		var me = this;

    	this.control({

			'knowledge-content-postinfoform': {
				addAttchment:{
					fn:this.addAttchment,
					scope:me
				},
				
				deleteAttchment: {
					fn:me.deleteAttachment,
					scope:me
				}
			}
						

    	});


    },
	
		
	loadInitComp : function() {
		var me = this;
		// --- bind store ---
		var dsCateStore = me.getKnowledgeCategoryCurrentTreeStore();
		dsCateStore.load();

		var fieldStores = {
			'dsCategory' : dsCateStore
		};
		
		var mainPanel = Ext.create('appmod.view.knowledge.content.PostInfoForm' , {
			region : 'center',
			border:false,
			padding:'0 0 0 0',
			fieldStores:fieldStores,
			buttonAlign : 'center',
			buttons: [{
	            text: '保 存',
				action:'postInfo'
        	},{
	            text: '重 置',
				action:'reset'
        	}
			]
	
		});
		return mainPanel;		
	},
	
	onLaunch:function(app) {
		var me = this;

		var formView = me.getPostInfoForm();
		
		
		var saveBtn = formView.down('button[action="postInfo"]');
		saveBtn.on("click",me.postInfo , me , {
			window : formView
		});

		
//		var addAttchmentBtn = formView.down('button[action="addAttachment"]');
//		addAttchmentBtn.on("click", me.addAttchment , me , {
//			window : formView,
//			formPanel : form
//		});
	},
	
	addAttchment:function(eOpts) {
		var me = this;
		
		var upWin = eOpts.window;
		var formPanel = eOpts.formPanel;
		var attchmentLayout = formPanel.down('fieldcontainer[name="attachmentLayout"]');
		var grid = formPanel.down('grid[name="attachmentGrid"]');
		//var data = grid.getStore().data;
		if(grid.getStore().data.length==0){
			var data = [{ 'name': eOpts.v,  "coller":"<a herf='#' >删除</a>"  }];
			var ds = Ext.create('Ext.data.Store', {
			    storeId:'simpsonsStore',
			    fields:['name', 'coller'],
			    data:data,
			    proxy: {
			        type: 'memory',
			        reader: {
			            type: 'json',
			            root: 'items'
			        }
			    }
			});
			grid.bindStore(ds);
			
		}else{
			grid.getStore().add({ 'name': eOpts.v,  "coller":"lisa@simpsons.com"  });
		}
		
	},
	
	deleteAttachment: function(eOpts){
		var grid = eOpts['grid'];
		var rowIndex = eOpts['rowIndex'];
		var colIndex = eOpts['colIndex'];

		var store = grid.getStore();
		store.removeAt(rowIndex);
	},
	
	addAttchment2: function(btn , event , eOpts) {
		var xp=new Ext.XTemplate("<input type=file id=fl style='display:none'>"); 
		var upWin = eOpts.window;
		var formPanel = eOpts.formPanel;
		var attchmentLayout = formPanel.down('fieldcontainer[name="attachmentLayout"]');
		attchmentLayout.add({
					xtype:'fieldcontainer',
					layout: 'hbox',
					margin: '10 0 0 0',
					items: [
						{
						xtype: 'filefield',
	                    name: 'attachment',
	                    anchor: '95%'
						},
						{
							xtype: 'button',
							text:'delete',
							listeners: {
						        click: function() {
									attchmentLayout.remove(this.ownerCt);
						            
						        }
						    }
						}
					]
                    
                });
		formPanel.doLayout();   
	},
	
	postInfo:function(btn , event , eOpts) {
		var me = this;
		
		var formPanel = eOpts.window;;

		
		var form = formPanel.getForm();
		form.submit({
			url:'../../data/resource/knowledge/content/submit_infoadding.json',
			//url:'http://www.baidu.com',
			success:function(form , action) {
				 // --- reflash store ---
				 
				 Ext.Msg.alert('成功', action.result.msg);
			},
			failure: function(form, action){
				switch (action.failureType) {
					case Ext.form.action.Action.CLIENT_INVALID:
						Ext.Msg.alert('失败', '表单里包含错误的信息');
						break;
					case Ext.form.action.Action.CONNECT_FAILURE:
						Ext.Msg.alert('失败', '服务器连接失败,请联系管理人员');
						break;
					case Ext.form.action.Action.SERVER_INVALID:
						Ext.Msg.alert('失败', action.result.msg);
				}
			}
		});
	}
	
	
	
});